<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>条码生成测试</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            padding: 20px;
            background: #f5f5f5;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: #fff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .test-section {
            margin-bottom: 40px;
            padding: 20px;
            border: 1px solid #e6e6e6;
            border-radius: 6px;
        }
        .test-section h2 {
            color: #333;
            margin-top: 0;
            border-bottom: 2px solid #1e9fff;
            padding-bottom: 10px;
        }
        .input-group {
            margin-bottom: 20px;
        }
        .input-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        .input-group input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }
        .btn {
            background: #1e9fff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        .btn:hover {
            background: #0d8ae6;
        }
        .result {
            margin-top: 20px;
            padding: 20px;
            background: #f8f9fa;
            border-radius: 4px;
            text-align: center;
            min-height: 120px;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
        }
        .barcode-info {
            margin-top: 10px;
            font-size: 14px;
            color: #666;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>条码生成测试</h1>
        
        <!-- EAN码测试 -->
        <div class="test-section">
            <h2>EAN-13条码测试</h2>
            <div class="input-group">
                <label for="eanInput">输入数字（12位，会自动计算校验位）：</label>
                <input type="text" id="eanInput" placeholder="例如：123456789012" maxlength="13">
            </div>
            <button class="btn" onclick="generateEAN()">生成EAN码</button>
            <div class="result" id="eanResult">
                <div style="color: #999;">请输入数字并点击生成按钮</div>
            </div>
        </div>
        
        <!-- UPC码测试 -->
        <div class="test-section">
            <h2>UPC-A条码测试</h2>
            <div class="input-group">
                <label for="upcInput">输入数字（11位，会自动计算校验位）：</label>
                <input type="text" id="upcInput" placeholder="例如：12345678901" maxlength="12">
            </div>
            <button class="btn" onclick="generateUPC()">生成UPC码</button>
            <div class="result" id="upcResult">
                <div style="color: #999;">请输入数字并点击生成按钮</div>
            </div>
        </div>
        
        <!-- 示例按钮 -->
        <div class="test-section">
            <h2>快速测试</h2>
            <button class="btn" onclick="testEANExample()" style="margin-right: 10px;">测试EAN示例</button>
            <button class="btn" onclick="testUPCExample()">测试UPC示例</button>
        </div>
    </div>

    <script>
        // 计算EAN-13校验位
        function calculateEANChecksum(digits) {
            var sum = 0;
            for (var i = 0; i < 12; i++) {
                var digit = parseInt(digits.charAt(i));
                if (i % 2 === 0) {
                    sum += digit;
                } else {
                    sum += digit * 3;
                }
            }
            var checksum = (10 - (sum % 10)) % 10;
            return checksum.toString();
        }
        
        // 计算UPC-A校验位
        function calculateUPCChecksum(digits) {
            var sum = 0;
            for (var i = 0; i < 11; i++) {
                var digit = parseInt(digits.charAt(i));
                if (i % 2 === 0) {
                    sum += digit * 3;
                } else {
                    sum += digit;
                }
            }
            var checksum = (10 - (sum % 10)) % 10;
            return checksum.toString();
        }
        
        // 验证并格式化EAN码
        function validateAndFormatEAN(input) {
            var digits = input.replace(/\D/g, '');
            
            if (digits.length === 0) {
                digits = '123456789012';
            }
            
            if (digits.length < 12) {
                while (digits.length < 12) {
                    digits = '0' + digits;
                }
            }
            
            if (digits.length > 13) {
                digits = digits.substring(0, 12);
            } else if (digits.length === 13) {
                digits = digits.substring(0, 12);
            }
            
            var checksum = calculateEANChecksum(digits);
            return digits + checksum;
        }
        
        // 验证并格式化UPC码
        function validateAndFormatUPC(input) {
            var digits = input.replace(/\D/g, '');
            
            if (digits.length === 0) {
                digits = '12345678901';
            }
            
            if (digits.length < 11) {
                while (digits.length < 11) {
                    digits = '0' + digits;
                }
            }
            
            if (digits.length > 12) {
                digits = digits.substring(0, 11);
            } else if (digits.length === 12) {
                digits = digits.substring(0, 11);
            }
            
            var checksum = calculateUPCChecksum(digits);
            return digits + checksum;
        }
        
        // 生成EAN码
        function generateEAN() {
            var input = document.getElementById('eanInput').value;
            var validEAN = validateAndFormatEAN(input);
            
            var apiUrls = [
                'https://api.qrserver.com/v1/create-barcode/?size=250x100&data=' + encodeURIComponent(validEAN) + '&type=ean13',
                'https://barcodeapi.org/api/ean13/' + encodeURIComponent(validEAN),
                'https://barcode.tec-it.com/barcode.ashx?data=' + encodeURIComponent(validEAN) + '&code=EAN13&multiplebarcodes=false&translate-esc=false&unit=Fit&dpi=96&imagetype=Gif&rotation=0&color=%23000000&bgcolor=%23ffffff&qunit=Mm&quiet=0'
            ];
            
            var html = '<div style="font-size: 12px; margin-bottom: 10px; color: #666;">EAN-13条码</div>';
            html += '<img src="' + apiUrls[0] + '" alt="EAN码" style="max-width: 100%; height: auto; border: 1px solid #ddd;" onerror="tryNextAPI(this, [\'' + apiUrls.join('\',\'') + '\'], \'EAN\', 0)">';
            html += '<div class="barcode-info">条码号：' + validEAN + '</div>';
            html += '<div class="barcode-info">格式：EAN-13 (包含校验位)</div>';
            
            document.getElementById('eanResult').innerHTML = html;
        }
        
        // 生成UPC码
        function generateUPC() {
            var input = document.getElementById('upcInput').value;
            var validUPC = validateAndFormatUPC(input);
            
            var apiUrls = [
                'https://api.qrserver.com/v1/create-barcode/?size=220x100&data=' + encodeURIComponent(validUPC) + '&type=upca',
                'https://barcodeapi.org/api/upca/' + encodeURIComponent(validUPC),
                'https://barcode.tec-it.com/barcode.ashx?data=' + encodeURIComponent(validUPC) + '&code=UPCA&multiplebarcodes=false&translate-esc=false&unit=Fit&dpi=96&imagetype=Gif&rotation=0&color=%23000000&bgcolor=%23ffffff&qunit=Mm&quiet=0'
            ];
            
            var html = '<div style="font-size: 12px; margin-bottom: 10px; color: #666;">UPC-A条码</div>';
            html += '<img src="' + apiUrls[0] + '" alt="UPC码" style="max-width: 100%; height: auto; border: 1px solid #ddd;" onerror="tryNextAPI(this, [\'' + apiUrls.join('\',\'') + '\'], \'UPC\', 0)">';
            html += '<div class="barcode-info">条码号：' + validUPC + '</div>';
            html += '<div class="barcode-info">格式：UPC-A (包含校验位)</div>';
            
            document.getElementById('upcResult').innerHTML = html;
        }
        
        // API切换函数
        function tryNextAPI(imgElement, apiUrls, type, currentIndex) {
            currentIndex++;
            if (currentIndex < apiUrls.length) {
                imgElement.src = apiUrls[currentIndex];
                imgElement.onerror = function() {
                    tryNextAPI(imgElement, apiUrls, type, currentIndex);
                };
            } else {
                imgElement.style.display = 'none';
                var fallbackDiv = document.createElement('div');
                fallbackDiv.innerHTML = '<div style="color: #f00;">条码生成失败，请检查网络连接</div>';
                imgElement.parentNode.appendChild(fallbackDiv);
            }
        }
        
        // 测试示例
        function testEANExample() {
            document.getElementById('eanInput').value = '123456789012';
            generateEAN();
        }
        
        function testUPCExample() {
            document.getElementById('upcInput').value = '12345678901';
            generateUPC();
        }
    </script>
</body>
</html> 